package mvc.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.bean.MvcBean;
import mvc.service.ActionBean;
import mvc.service.ActionObject;

public class Action extends HttpServlet{


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * post方法
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
		String username=request.getParameter("username");
		String password=request.getParameter("password");
        String path=request.getServletPath();
        path=path.split("/")[1];
        Map<String,MvcBean> map=(Map<String,MvcBean>)request.getServletContext().getAttribute("mvc");
        MvcBean bean=map.get(path);
        ActionObject object=ActionBean.actionBean(bean.getBeanClass(), request);
        ActionInterface actionInterface=null;
        String type=bean.getType();
        String url="";
        try{
        	Class clazz=Class.forName(type);
        	actionInterface=(ActionInterface)clazz.newInstance();
        	url=actionInterface.execute(request, response, object, bean.getMap());
        }catch(Exception e){
        	e.printStackTrace();
        }
		RequestDispatcher dis=request.getRequestDispatcher(url);
		dis.forward(request, response);
	}
	
	/**
	 * get方法
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		this.doPost(request, response);
	}
	
}
